package com.kehao.leetcode.middle.backtracking_algorithm;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class PermuteSolution {

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            temp.add(nums[i]);
        }
        fullSort(nums,0,result,temp);
        return result;
    }

    private void fullSort(int[] nums, int depth, List<List<Integer>> result, List<Integer> temp) {
        if(depth==nums.length-1){
            result.add(new ArrayList<>(temp));
        }else {
            fullSort(nums,depth+1,result,temp);
            for (int i = depth+1; i < nums.length; i++) {
                swap(depth,i,temp);
                fullSort(nums,depth+1,result,temp);
                swap(depth,i,temp);
            }
        }
    }

    private void swap(int depth, int i, List<Integer> temp) {
        Integer swaptemp = temp.get(depth);
        temp.set(depth,temp.get(i));
        temp.set(i,swaptemp);
    }


    @Test
    public void test01(){
        int[] nums = {1,2,3};
        System.out.println(permute(nums));
    }

    @Test
    public void test02(){
        int[] nums = {0,1};
        System.out.println(permute(nums));
    }

    @Test
    public void test03(){
        int[] nums = {0};
        System.out.println(permute(nums));
    }

    @Test
    public void test04(){
        int[] nums = {};
        System.out.println(permute(nums));
    }
}
